package com.example.job_backend.controller;

import com.alibaba.excel.EasyExcel;
import com.example.job_backend.model.R;
import com.example.job_backend.model.User;
import com.example.job_backend.service.UserService;
import com.example.job_backend.util.PasswordUtils;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserImportController {
    @Resource
    private UserService userService;

    @PostMapping("/importUsers")
    public R importStudents(@RequestParam("file") MultipartFile file) throws IOException {
        // 使用 EasyExcel 读取 Excel 数据
        List<User> users = EasyExcel.read(file.getInputStream())
                .head(User.class)
                .sheet()
                .doReadSync();
//        System.out.println(users);
        //密码加密
        String passwordHash = PasswordUtils.md5WithSalt("123456", "04943c669d0e4ca2830e402ceaa6242d");

        // 遍历并处理每个 ExcelStudentData
        for (User user : users) {
            user.setPassword(passwordHash);
            // 调用 Service 方法插入数据
            userService.save(user);
        }
        return new R(200, "请求成功", null);
    }
}
